<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>去除字符串首尾处的空白字符（或者其他字符）</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.substr.html">« substr</a></li>
      <li style="float: right;"><a href="function.ucfirst.html">ucfirst »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.strings.html">字符串 函数</a></li>
    <li>去除字符串首尾处的空白字符（或者其他字符）</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.trim" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">trim</h1> 
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">trim</span> &mdash; <span class="dc-title">去除字符串首尾处的空白字符（或者其他字符）</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.trim-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>trim</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$characters</code><span class="initializer"> = &quot; \n\r\t\v\x00&quot;</span></span>): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   此函数返回字符串 <code class="parameter">string</code> 去除首尾空白字符后的结果。如果不指定第二个参数，<span class="function"><strong>trim()</strong></span> 将去除这些字符：
   
   <ul class="itemizedlist">
    <li class="listitem">
     <span class="simpara">
      &quot; &quot; (<abbr title="American Standard Code for Information Interchange">ASCII</abbr> <code class="literal">32</code> 
      (<code class="literal">0x20</code>))，普通空格符。
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      &quot;\t&quot; (<abbr title="American Standard Code for Information Interchange">ASCII</abbr> <code class="literal">9</code> 
      (<code class="literal">0x09</code>))，制表符。
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      &quot;\n&quot; (<abbr title="American Standard Code for Information Interchange">ASCII</abbr> <code class="literal">10</code> 
      (<code class="literal">0x0A</code>))，换行符。
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      &quot;\r&quot; (<abbr title="American Standard Code for Information Interchange">ASCII</abbr> <code class="literal">13</code> 
      (<code class="literal">0x0D</code>))，回车符。
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      &quot;\0&quot; (<abbr title="American Standard Code for Information Interchange">ASCII</abbr> <code class="literal">0</code> 
      (<code class="literal">0x00</code>))，空字节符。
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      &quot;\v&quot; (<abbr title="American Standard Code for Information Interchange">ASCII</abbr> <code class="literal">11</code> 
      (<code class="literal">0x0B</code>))，垂直制表符。
     </span>
    </li>
   </ul>
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.trim-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">string</code></dt>

     <dd>

      <p class="para">
       待处理的<span class="type"><span class="type 字符串">字符串</span></span>。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">characters</code></dt>

     <dd>

      <p class="para">
      可选参数，过滤字符也可由 <code class="parameter">characters</code> 参数指定。一般要列出所有希望过滤的字符，也可以使用 “<code class="literal">..</code>” 列出一个字符范围。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.trim-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   过滤后的字符串。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.trim-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4879">
    <p><strong>示例 #1 <span class="function"><strong>trim()</strong></span> 使用范例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$text&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"\t\tThese&nbsp;are&nbsp;a&nbsp;few&nbsp;words&nbsp;:)&nbsp;...&nbsp;&nbsp;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$binary&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"\x09Example&nbsp;string\x0A"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$hello&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;World"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$binary</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$hello</span><span style="color: #007700">);<br /><br />print&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$trimmed&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$trimmed</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$trimmed&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"&nbsp;\t."</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$trimmed</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$trimmed&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">$hello</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Hdle"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$trimmed</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;清除&nbsp;$binary&nbsp;首位的&nbsp;ASCII&nbsp;控制字符<br />//&nbsp;（包括&nbsp;0-31）<br /></span><span style="color: #0000BB">$clean&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">$binary</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"\x00..\x1F"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$clean</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(32) &quot;        These are a few words :) ...  &quot;
string(16) &quot;    Example string
&quot;
string(11) &quot;Hello World&quot;

string(28) &quot;These are a few words :) ...&quot;
string(24) &quot;These are a few words :)&quot;
string(5) &quot;o Wor&quot;
string(14) &quot;Example string&quot;
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4880">
    <p><strong>示例 #2 使用 <span class="function"><strong>trim()</strong></span> 清理数组值</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">trim_value</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;<br />{&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">);&nbsp;<br />}<br /><br /></span><span style="color: #0000BB">$fruit&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">,</span><span style="color: #DD0000">'banana&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&nbsp;cranberry&nbsp;'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruit</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">array_walk</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruit</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'trim_value'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruit</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(3) {
  [0]=&gt;
  string(5) &quot;apple&quot;
  [1]=&gt;
  string(7) &quot;banana &quot;
  [2]=&gt;
  string(11) &quot; cranberry &quot;
}
array(3) {
  [0]=&gt;
  string(5) &quot;apple&quot;
  [1]=&gt;
  string(6) &quot;banana&quot;
  [2]=&gt;
  string(9) &quot;cranberry&quot;
}

</pre></div>
    </div>
   </div>
  </p>
 </div>



 <div class="refsect1 notes" id="refsect1-function.trim-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <strong>可能的问题：删除中间字符</strong><br />
   <p class="para">
    因为 <span class="function"><strong>trim()</strong></span>
    从字符串的开始和末尾修剪字符。所以中间是否删除字符可能会造成混淆。<code class="literal">trim(&#039;abc&#039;, &#039;bad&#039;)</code>
    移除了“a”和“b”，是因为修剪了“a”之后，“b”移动到了开头，所以也被修剪了。所以这就是为什么能正常工作而
    <code class="literal">trim(&#039;abc&#039;, &#039;b&#039;)</code> 看起来没有的原因。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.trim-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.ltrim.html" class="function" rel="rdfs-seeAlso">ltrim()</a> - 删除字符串开头的空白字符（或其他字符）</span></li>
    <li class="member"><span class="function"><a href="function.rtrim.html" class="function" rel="rdfs-seeAlso">rtrim()</a> - 删除字符串末端的空白字符（或者其他字符）</span></li>
    <li class="member"><span class="function"><a href="function.str-replace.html" class="function" rel="rdfs-seeAlso">str_replace()</a> - 子字符串替换</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>